using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.SQLite;
using System.Windows.Forms;
using System.Diagnostics;

namespace Pix2Gallery2
{
    public partial class PSEOptionsForm : Form
    {
        private Pix2Gallery2Settings pix2gallery2Settings;
        //private Pix2Gallery2Settings m_pix2gallery2Settings;
        public PSEOptionsForm()
        {
            InitializeComponent();
        }

        private void PSEOptionsForm_Load(object sender, EventArgs e)
        {
            pix2gallery2Settings = new Pix2Gallery2Settings();
            PSE6Interface.PhotoItemInfo pii = new PSE6Interface.PhotoItemInfo();
            DataTable dt = pii.GetItemInfo();
            dt.Rows.Add("-99", "", "text", "0");
            dt.Rows.Add("-75", "All Tags", "text", "0");
            dt.Rows.Add("-70", "All Album Names", "text", "0");
            DataView v = dt.DefaultView;
            v.Sort = "id ASC";
            dt = v.ToTable();
            //
            DataTable dtclbTitle = v.ToTable();
            clbTitle.DataSource = dtclbTitle;
            clbTitle.DisplayMember = "identifier";
            clbTitle.ValueMember = "id";
            clbTitle.SelectedIndex = 0;

            DataTable dtclbSummary = v.ToTable();
            clbSummary.DataSource = dtclbSummary;
            clbSummary.DisplayMember = "identifier";
            clbSummary.ValueMember = "id";
            clbSummary.SelectedIndex = 0;

            DataTable dtclbDescription = v.ToTable();
            clbDescription.DataSource = dtclbDescription;
            clbDescription.DisplayMember = "identifier";
            clbDescription.ValueMember = "id";
            clbDescription.SelectedIndex = 0;

            if (this.pix2gallery2Settings.LoadAppSettings())
            {
                //if ((pix2gallery2Settings.PSE_TitlePopulatedBy != "") && (pix2gallery2Settings.PSE_TitlePopulatedBy != null))
                //    this.cmbTitleFrom.SelectedIndex = Convert.ToInt16(pix2gallery2Settings.PSE_TitlePopulatedBy);
                //else
                //    this.cmbTitleFrom.SelectedIndex = 1;
                //if ((pix2gallery2Settings.PSE_SummaryPopulatedBy != "") && (pix2gallery2Settings.PSE_SummaryPopulatedBy != null))
                //    this.cmbSummaryFrom.SelectedIndex = Convert.ToInt16(pix2gallery2Settings.PSE_SummaryPopulatedBy);
                //else
                //    this.cmbSummaryFrom.SelectedIndex = 0;
                //if ((pix2gallery2Settings.PSE_DescriptionPopulatedBy != "") && (pix2gallery2Settings.PSE_DescriptionPopulatedBy != null))
                //    this.cmbDescriptionFrom.SelectedIndex = Convert.ToInt16(pix2gallery2Settings.PSE_DescriptionPopulatedBy);
                //else
                //    this.cmbDescriptionFrom.SelectedIndex = 0;
                if ((pix2gallery2Settings.PSE_DefaultUploadPictureSize != "") && (pix2gallery2Settings.PSE_DefaultUploadPictureSize != null))
                    this.cmbDefaultPictureSize.SelectedIndex = Convert.ToInt16(pix2gallery2Settings.PSE_DefaultUploadPictureSize);
                else
                    this.cmbDefaultPictureSize.SelectedIndex = 0;
                this.ckbDefaultConvertToJPG.Checked = pix2gallery2Settings.PSE_DefaultConvertToJPG;
                this.cbxShowUsageInstructionsOnStartup.Checked = !pix2gallery2Settings.PSE_HideInstructionsOnStartup;
                //20080925 wcf handle multi choicesfor PSE 2 gallery 2 fields
                if (pix2gallery2Settings.PseField2Gallery2Fields.Count > 0)
                {
                    SetPSE2GalCheckBoxes("TITLE", ref clbTitle);
                    SetPSE2GalCheckBoxes("SUMMARY", ref clbSummary);
                    SetPSE2GalCheckBoxes("DESCRIPTION", ref clbDescription);
                }
                this.chkbxLongTitle.Checked = pix2gallery2Settings.UseLongTitles;
                this.chkbxLongSummary.Checked = pix2gallery2Settings.UseLongSummaries;
                this.chkbxLongDescription.Checked = pix2gallery2Settings.UseLongDescriptions;

            }
            ckbDefaultConvertToJPG.Enabled = (this.cmbDefaultPictureSize.SelectedIndex == 0);
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            //20081005 wcf clear the old multi-select settings
            pix2gallery2Settings.PseField2Gallery2Fields = null;
            pix2gallery2Settings.PseField2Gallery2Fields = new List<PseField2Gallery2Field>();
            AddPSE2GalItems("TITLE", ref clbTitle);
            AddPSE2GalItems("SUMMARY", ref clbSummary);
            AddPSE2GalItems("DESCRIPTION", ref clbDescription);
            pix2gallery2Settings.AppSettingsChanged = true;
            this.pix2gallery2Settings.SaveAppSettings();
            if (this.pix2gallery2Settings.lastErrorMessage != "")
            {
                MessageBox.Show("Error: " + this.pix2gallery2Settings.lastErrorMessage);
            }
            this.Close();
        }
        private void AddPSE2GalItems(string strgalleryfield, ref CheckedListBox clbOptions)
        {


            foreach (DataRowView checkedRow in clbOptions.CheckedItems)
            {
                //20080926 wcf I like this new syntax.  See:
                //http://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspx
                //pix2gallery2Settings.PseField2Gallery2Fields.Add(new PseField2Gallery2Field { m_id = checkedRow["id"].ToString(), m_identifier = checkedRow["identifier"].ToString(), m_data_type = checkedRow["data_type"].ToString(), m_multi_item = checkedRow["multi_item"].ToString(), m_gallery2field = strgalleryfield });
                //20081031 TED - I agree, but alas I am still using VS2005, reverting to the "old", less elegant syntax...
                PseField2Gallery2Field PseField2 = new PseField2Gallery2Field();
                PseField2.m_id = checkedRow["id"].ToString();
                PseField2.m_identifier = checkedRow["identifier"].ToString();
                PseField2.m_data_type = checkedRow["data_type"].ToString();
                PseField2.m_multi_item = checkedRow["multi_item"].ToString();
                PseField2.m_gallery2field = strgalleryfield;
                pix2gallery2Settings.PseField2Gallery2Fields.Add(PseField2);
            }
        }
        private void SetPSE2GalCheckBoxes(string strgalleryfield, ref CheckedListBox clbOptions)
        {
            foreach (PseField2Gallery2Field pse2g2f in this.pix2gallery2Settings.PseField2Gallery2Fields)
            {
                foreach (DataRowView row2Check in clbOptions.Items)
                {
                    if (row2Check["identifier"].ToString() == pse2g2f.m_identifier && strgalleryfield == pse2g2f.m_gallery2field)
                    {
                        clbOptions.SetItemChecked(clbOptions.Items.IndexOf(row2Check), true);
                        break;
                    }
                }
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //private void cmbTitleFrom_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    pix2gallery2Settings.PSE_TitlePopulatedBy = this.cmbTitleFrom.SelectedIndex.ToString();
        //}

        //private void cmbSummaryFrom_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    pix2gallery2Settings.PSE_SummaryPopulatedBy = this.cmbSummaryFrom.SelectedIndex.ToString();
        //}

        //private void cmbDescriptionFrom_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    pix2gallery2Settings.PSE_DescriptionPopulatedBy = this.cmbDescriptionFrom.SelectedIndex.ToString();
        //}

        private void cmbDefaultPictureSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            pix2gallery2Settings.PSE_DefaultUploadPictureSize = this.cmbDefaultPictureSize.SelectedIndex.ToString();
            this.UpdateDefaultConvertToJPGEditable();
        }

        private void ckbDefaultConvertToJPG_CheckedChanged(object sender, EventArgs e)
        {
            pix2gallery2Settings.PSE_DefaultConvertToJPG = this.ckbDefaultConvertToJPG.Checked;
        }
        private void UpdateDefaultConvertToJPGEditable()
        {
            //Function will update the Convert To JPG field to editable (or not)
            ckbDefaultConvertToJPG.Enabled = (this.cmbDefaultPictureSize.SelectedIndex == 0);
            //pix2gallery2Settings.PSE_DefaultConvertToJPG = this.ckbDefaultConvertToJPG.Checked;
        }

        private void cbxShowUsageInstructionsOnStartup_CheckedChanged(object sender, EventArgs e)
        {
            pix2gallery2Settings.PSE_HideInstructionsOnStartup = !this.cbxShowUsageInstructionsOnStartup.Checked;
        }

        private void chkbxLongTitle_CheckedChanged(object sender, EventArgs e)
        {
            pix2gallery2Settings.UseLongTitles = chkbxLongTitle.Checked;
        }

        private void chkbxLongSummary_CheckedChanged(object sender, EventArgs e)
        {
            pix2gallery2Settings.UseLongSummaries = chkbxLongSummary.Checked;
        }

        private void chkbxLongDescription_CheckedChanged(object sender, EventArgs e)
        {
            pix2gallery2Settings.UseLongDescriptions = chkbxLongDescription.Checked;
        }
    }
}
